/**
* @file catlog_port.h in catlog
* @author linfe
* @comment
* Create on 2023/3/16 22:57
* @version 1.0
**/

#ifndef CATLOG__CATLOG_PORT_H_
#define CATLOG__CATLOG_PORT_H_
#include "catlog_conf.h"

#ifdef CATLOG_PLATFORM_RTOS
#define catlog_malloc bsp_malloc
#else
#define catlog_malloc malloc
#endif


#ifdef __GNUC__
#include <stdint.h>
#endif

#if CATLOG_MUTEX_LOCK_ENABLE == 1
#ifdef __GNUC__
#if defined(linux) || defined(WIN32)
#include <pthread.h>
#include <memory.h>
typedef pthread_mutex_t catlog_mutex_t;
#else
/*FreeRTOS*/
#include "bsp_board.h"
#include <memory.h>
typedef SemaphoreHandle_t* catlog_mutex_t;
#endif  /*end of OS*/
#endif  /*end of compiler*/
#endif  /*end of CATLOG_MUTEX_LOCK_ENABLE*/


void catlog_mutex_init(void *mutex);
void catlog_mutex_lock(void *mutex);
void catlog_mutex_unlock(void *mutex);



/**
 * write timestamp to string buffer
 * @param buffer pointer to string
 * @return number of characters
 */
uint32_t catlog_get_sys_time_str(char *buffer);

/**
 * open stdout or stderr on windows or linux
 */
void catlog_open_stderr(void *);

/**
 * close stdout or stderr on windows or linux
 */
void catlog_close_stderr(void *);

/**
 * open file
 * @param filename pointer to filename
 */
void catlog_open_file(void *filename);

/**
 * close file
 */
void catlog_close_file(void *);

/**
 * open device
 * @param dev_name pointer to device name such as "/dev/ttyUSB0"
 */
void catlog_open_device(void *dev_name);

/**
 * close device
 */
void catlog_close_device(void *);

/**
 * write buffer to stderr
 * @param buffer pointer to log buffer
 * @param size size of valid data
 */
void catlog_write_to_stderr(uint8_t * buffer,uint32_t size);

/**
 * write buffer to file
 * @param buffer pointer to log buffer
 * @param size size of valid data
 */
void catlog_write_to_file(uint8_t * buffer,uint32_t size);

/**
 * write buffer to device
 * @param buffer pointer to log buffer
 * @param size size of valid data
 */
void catlog_write_to_device(uint8_t *buffer,uint32_t size);

#endif //CATLOG__CATLOG_PORT_H_
